/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.bikerz.user.view;


import br.com.bikerz.user.domain.User;
import br.com.bikerz.user.service.UserService;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("createUser")
@Scope("request")
public class CreateUser implements Serializable{

	private User user = new User();

	private UserService userService;

	public CreateUser() {}

	@Autowired
	public CreateUser(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void save(ActionEvent actionEvent) {
                user.setStatus(true);
		userService.createNew(user);

		/*FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "User is saved");
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		user = new User();*/

                FacesContext context = FacesContext.getCurrentInstance();
                NavigationHandler navHandler = context.getApplication().getNavigationHandler();
                navHandler.handleNavigation(context, null, "/pages/profile");
	}
}
